package com.xquant.pricing.calc.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.xquant.common.core.web.controller.BaseController;
import com.xquant.common.engine.xPP.request.CalcEngineParam;
import com.xquant.common.engine.xPP.request.CalcInstrumentForm;
import com.xquant.common.engine.xPP.request.CalcPricingForm;
import com.xquant.common.engine.xPP.request.CalcResultForm;
import com.xquant.common.security.annotation.RequiresPermissions;
import com.xquant.common.security.utils.SecurityUtils;
import com.xquant.pricing.calc.service.CalcLogsService;
import com.xquant.pricing.utils.CommResponse;
import com.xquant.pricing.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: B946
 * @Date: 18/03/2020 11:11
 * @Description:
 */
@RestController
@RequestMapping(value = "calcLogs")
public class CalcLogsController extends BaseController {

    @Autowired
    private CalcLogsService calcLogsService;


    @RequestMapping(value = "/queryCalcLogs.action")
    @ResponseBody
    public Object queryCalcLogs(HttpServletRequest request, @RequestParam Map<String, Object> params) {
        startPage();
        params.put("userId", SecurityUtils.getUserId());
        List list = calcLogsService.queryCalcLogs(params);
        return getDataTable(list);
    }

    @RequestMapping(value = "/saveCalcLogs.action")
    @ResponseBody
    public Object saveCalcLogs(@RequestBody CalcEngineParam calcEngineParam) {
        // 业务系统请求计算
        Map params = new HashMap();
        CalcInstrumentForm calcInstrumentForm = calcEngineParam.getCalcInstrumentForm();
        CalcPricingForm calcPricingForm = calcEngineParam.getCalcPricingForm();
        CalcResultForm calcResultForm = calcEngineParam.getCalcResultForm();
        params.put("draftName", calcEngineParam.getDraftName());
        if (calcLogsService.queryCalcLogsByName(params).size() > 0) {
            return new CommResponse(false, "名称已存在，请检查！");
        }
        params.put("productType", "OtcOption");
        params.put("initOptionType", calcInstrumentForm.getInitProductType());
        params.put("calcType", "");
        params.put("operateDate", DateUtils.getCurr_YYYY_MM_DD());
        params.put("valueDate", calcEngineParam.getValueDate());
        params.put("calcDate", "");
        params.put("optionType", calcInstrumentForm.getProductType());
        params.put("instrument", calcInstrumentForm.getInstrumentId());
        params.put("partyId", calcEngineParam.getPartyId());
        params.put("pForm", JSONUtil.toJsonStr(calcEngineParam));
        params.put("pFormInstrument", JSONUtil.toJsonStr(calcInstrumentForm));
        params.put("pFormPricing", JSONUtil.toJsonStr(calcPricingForm));
        params.put("pFormResult", JSONUtil.toJsonStr(calcResultForm));
        params.put("pCalcInstrument", JSONUtil.toJsonStr(calcEngineParam.getInstrument()));
        params.put("pCalcPricing", JSONUtil.toJsonStr(calcEngineParam.getCalcPricingParam()));
        params.put("pCalc", "");
        params.put("pCalcInit", "");
        params.put("reqTime", "");
        params.put("pResponse", "");
        params.put("resTime", "");
        params.put("userId", SecurityUtils.getUserId());
        params.put("calcServer", "");
        if (ObjectUtil.isNotNull(calcInstrumentForm.getInstrumentId())) {
            params.put("iCode", calcInstrumentForm.getInstrumentId().split(",")[0]);
            params.put("aType", calcInstrumentForm.getInstrumentId().split(",")[1]);
            params.put("mType", calcInstrumentForm.getInstrumentId().split(",")[2]);
        } else {
            params.put("iCode", "");
            params.put("aType", "");
            params.put("mType", "");
        }
        params.put("logType", "1");
        params.put("remark", calcEngineParam.getRemark());
        params.put("status", "1");
        calcLogsService.saveCalcLog(params);
        return CommResponse.success();
    }

    /**
     * 删除日志
     */
    @RequiresPermissions("calc:otcOtion:pricingdraft:remove")
    @RequestMapping(value = "/deleteCalcLogs.action")
    public CommResponse deleteCalcLogs(@RequestParam Map<String, Object> param) {
        param.put("userId", SecurityUtils.getUserId());
        calcLogsService.deleteCalcLogs(param);
        return new CommResponse(true, "操作成功");
    }


}
